/* $Id: OutOfSyncHandler.java,v 1.2 2010/05/27 18:51:45 nhnb Exp $ */
/***************************************************************************
 *                   (C) Copyright 2003-2010 - Marauroa                    *
 ***************************************************************************
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
package marauroa.server.game.messagehandler;

import games.mapacman.server.MaPacmanRPWorld;
import java.util.Vector;
import marauroa.common.Log4J;
import marauroa.common.game.RPObject;
import marauroa.common.net.message.Message;
import marauroa.common.net.message.MessageC2SOutOfSync;
import marauroa.common.net.message.MessageS2CMigrate;
import marauroa.common.net.message.MessageS2SInSync;
import marauroa.server.game.GameServerManager;
import marauroa.server.game.SimulationStats;
import marauroa.server.game.container.ClientState;
import marauroa.server.game.container.PlayerEntry;

/**
 * When client get out of sync because it has lost part
 * of the messages stream, it can request a
 * synchronization to continue game.
 */
//TODO: Consider removing this message as with TCP it won't fail.
class MigrationHandler extends MessageHandler {

    /** the logger instance. */
    private static final marauroa.common.Logger logger = Log4J.getLogger(GameServerManager.class);

    /**
     * This message is send from client to notify that client suffered a network
     * problem and request data to be resend again to it.
     *
     * @param message
     *            the out of sync message
     */
    @Override
    public void process(Message message) {
        MessageS2CMigrate msg = (MessageS2CMigrate) message;
        try {
            int clientid = msg.getClientID();
            Vector<RPObject> players = MaPacmanRPWorld.get().getPlayers();
            synchronized (players) {
                for (RPObject player : players) {
                    if (player.get("name").compareTo(msg.getClientName()) == 0) {
                        playerContainer.getLock().releaseLock();
                        playerContainer.getLock().requestWriteLock();
                        PlayerEntry entry = playerContainer.add(msg.getSocketChannel());
                        entry.setObject(player);
                        //entry.clientid = clientid;
                        entry.state = ClientState.GAME_BEGIN;
                        entry.character = msg.getClientName();
                        entry.username = "test" + msg.getClientName().substring(4);
                        playerContainer.getLock().releaseLock();

                    }
                }
            }
            SimulationStats.getStatistics().add("m_end", ""+System.currentTimeMillis());
            SimulationStats.getStatistics().print();

        } catch (Exception e) {
            logger.error("error while processing OutOfSyncEvent", e);
        }
    }
}
